﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using LLClient.ClientServiceRef;
using LLClient.ServerProxy;

namespace LLClient.Portal
{
    /// <summary>
    /// Interaction logic for Welcome.xaml
    /// </summary>
    public partial class Welcome : Window
    {
        private Proxy _proxy = Proxy.GetInstance();

        public Welcome()
        {
            InitializeComponent();
            Proxy.Instance.ProxyConnect += ProxyConnectEvent;
        }

        void ProxyConnectEvent(object sender, ProxyConnectEventArgs e)
        {
            if (!this.CheckAccess())
            {
                this.Dispatcher.BeginInvoke(new Action(() => this.ProxyConnectEvent(sender, e)));
                return;
            }

            if (e.Connected)
            {
                Proxy.Instance.ProxyConnect -= ProxyConnectEvent;

                MainMenu mainMenuWindow = new MainMenu();
                mainMenuWindow.Show();

                this.Close();
            }
            else
            {
                MessageBox.Show("Login failed");
            }
        }

        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            string username = usernameBox.Text;
            string password = passwordBox.Password;

            Client client = new Client();
            client.Username = username;
            client.Password = password;
            client.IsAvailable = true;
            
            _proxy.Connect(client);
        }

        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            Registration reg = new Registration();
            reg.Show();
            this.Close();
        }
    }
}
